//
//  PSLoginViewController.m
//  psico
//
//  Created by wagner silva on 09/07/13.
//  Copyright (c) 2013 ws. All rights reserved.
//

#import "PSLoginViewController.h"

@interface PSLoginViewController ()

@end

@implementation PSLoginViewController

@synthesize txtEmail;
@synthesize txtSenha;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)recuarTeclado:(id)sender{
    [sender resignFirstResponder];

}

- (IBAction)clicarNoFundo:(id)sender{
    [txtEmail resignFirstResponder];
    [txtSenha resignFirstResponder];
}

- (IBAction)limparEmail:(id)sender {
    txtEmail.text=@"";
}

- (IBAction)limparSenha:(id)sender {
    txtSenha.text = @"";
}

- (IBAction)onClickLogin:(id)sender {
     if ([txtEmail.text isEqualToString:@""] || [txtSenha.text isEqualToString:@""]) {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Preencha todos os campos" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
     [alert show];
     return;
     }
     // i will use a code from connect to DB tutorial
     NSString *strURL = [NSString stringWithFormat:@"http://localhost/pslogin.php?userName=%@&password=%@",txtEmail.text.lowercaseString, txtSenha.text];
     
     // to execute php code
     NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
     
     // to receive the returend value
     NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
     
     if ([strResult isEqualToString:@"1"])
     {
    
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Deu tudo certo" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
         [alert show];
     }
     else
     {
         // invalid information
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Informação Inválida" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
         [alert show];
     
         return;
     }
}

@end
